bmgadget =
{	
	config:
	{
		username : "",
		passwd : "",
		read : function()
		{
			this.username = System.Gadget.Settings.readString( "username" );
			this.passwd = System.Gadget.Settings.readString( "passwd" );						
		},
		save : function()
		{
			System.Gadget.Settings.writeString( "username", username );
			System.Gadget.Settings.writeString( "passwd", passwd );
		}
	},
	settings:
	{
		display : function()
		{
			usernameField.value = bmgadget.config.username;			
		},
		update : function()
		{
			bmgadget.config.username = usernameField.value;
			
			if ( passwordField.value != '' )
			{
				bmgadget.config.passwd = passwordField.value;
			}
		}
	},
	flyout:
	{
		display : function()
		{
		},
		keypress : function()
		{
		}
	},
	dockpanel:
	{
		keypress: function()
		{
		    if (System.Gadget.docked) return;
		    
		    switch(event.keyCode)
		    {
		        case 37:
		        case 100:
		            bmgadget.onAction("prev");
		            break;
		        case 39:
		        case 102:
		            bmgadget.onAction("next");
		            break;		        
		    }		   
		}
	},
	init : function()
	{
		service = new ActiveXObject( "BackgroundMotionGadgetServices.ContentServiceClient" );
		service.IsSecure = true;
		service.RequiresAuthentication = true;
		service.Username = "jb@mindscape.co.nz";
		service.Password = "password";
		service.Address = "http://localhost:82/ContentService";
	
		this.reload();
	},
	reload : function()
	{
		try
		{
			this.unmoderatedItems = service.GetContentForModeration();
			dockmessage.innerHTML = this.unmoderatedItems.Count + " items pending";
			this.currentIndex = 0;
		}
		catch (ex)
		{
			dockmessage.innerHTML = "BadCredentials";
		}
	},
	display : function()
	{
		if ( System.Gadget.docked ) return;
		if ( bmgadget.unmoderatedItems == null ) return;
		
		undockpicture.src = bmgadget.unmoderatedItems.GetItem(bmgadget.currentIndex).PreviewImage;
		undocktitle.innerHTML = bmgadget.unmoderatedItems.GetItem(bmgadget.currentIndex).Title;
			
		undocklink.href = "http://backgroundmotion/View.aspx?id=" + bmgadget.unmoderatedItems.GetItem(bmgadget.currentIndex).Id;
	},
	getNextItem : function()
	{	
		bmgadget.currentIndex++;		
		if ( bmgadget.currentIndex >= bmgadget.unmoderatedItems.Count ) bmgadget.currentIndex = 0;													
	},
	getPrevItem : function()
	{			
		bmgadget.currentIndex--;		
		if ( bmgadget.currentIndex < 0 ) bmgadget.currentIndex = bmgadget.unmoderatedItems.Count - 1;
	},	
	load : function()
	{
	},
	docked : function()
	{		
	},
	undocked : function()
	{
		this.display();
	},
	onAction: function(action)
	{
            switch(action)
            {
        	case "prev":
                    this.getPrevItem();	                    
                    break;
                case "next":	                    
                    this.getNextItem();
                    break;
                case "approve":
                    this.approveCurrentItem();
                    break;	               
	    }  		
	    
	    this.display();    		    
	},
	approveCurrentItem : function()
	{
		service.ApproveContent(bmgadget.unmoderatedItems.GetItem(bmgadget.currentIndex).Id);
		this.reload();		
		this.display();
	},
	currentIndex : 0,
	unmoderatedItems : null,
	service : null
}